Sample 15 Roots Newton-Raphson




DLG(TI="Newton-Raphson Root Finding", Edit='#Describe',W=3, LBL='ESCape to see code', B='OK')

#Describe . This sample solves the equations for x . SIN(x) = 0, x <= 0.001 <= 6 . SIN(x) = COS(x), x1 <= 0 <= 3 . SIN(x) = COS(x), x1 <= 3 <= 6 . Call ROOT: . xZero = ROOT("y=func(x)", "dy=dFunc(x)", . [lower, upper, precision, maxIters, ivalParts]) . "y=func(x)" = Function to get roots from (obligatory) . "y=dFunc(x)" = 1st derivative of func (obligatory) . (See script on how to build func and dFunc) . lower = lower bound for root, default=0 (optional) . upper = upper bound for root, default=1 (optional) . precision = Desired accuracy, default=1E-8 (optional) . maxIters = maximum iterations . Results are plotted with functions F3 and F4. . (F functions can be called with its key or by mouse) . The Newton-Raphson algorithm . is used the find the roots of . func(x) = 0
. help the compiler and make variables numerical: SET(x=0, y=0, dy=0) . find the x=$x0 between 0.001 and 6: . Note: the $ makes a variable global $x0 = ROOT("y=SINeqNULL(x)", "dy=dSINeqNULL(x)", 0.001, 6) SIN($x0) F3() ! plot SIN and $x0 == pi . find x=$x1 for SIN(x) = COS(x) between x=0 and x=3 $x1 = ROOT("y=SINeqCOS(x)", "dy=dSINeqCOS(x)", 0, 3) SINeqCOS($x1) ! x1 should be close to zero . find x=$x2 for SIN(x) = COS(x) between x=3 and x=6 $x2 = ROOT("y=SINeqCOS(x)", "dy=dSINeqCOS(x)", 3, 6, 1E-15, 100) SINeqCOS($x2) ! x2 should be close to zero F4() ! plot SIN and COS and $x1 and $x2 DLG(T=0.9, B="Quit HicEst",Y=0,X=1,BG=900) IF($txtRC == "Quit HicEst") SYSTEM(Quit=1) END FUNCTION SINeqNULL(xx) SINeqNULL = SIN(xx) END FUNCTION dSINeqNULL(xx) dSINeqNULL = COS(xx) END FUNCTION SINeqCOS(x) SINeqCOS= SIN(x) - COS(x) END FUNCTION dSINeqCOS(x) dSINeqCOS = COS(x) + SIN(x) END FUNCTION F3() ! plot SIN SET(xmin=0, xmax=6) xtitle = "black: $x0 = PI = 3.1415" DLG(TI="SIN(x) == 0", Rows=2,Cols=2, Ax=2,TI=xtitle, Min=xmin,Max=xmax, Y=0,TI="SIN(x)", MIN=-1,Max=1) DO i = 1, 100 X(i) = xmin + i * (xmax - xmin) / 100 sinus(i) = SIN(X(i)) ENDDO LINE(Ax=2, X=$x0,Y=-1,Draw=-1, Y=1,W=4,Draw=0) LINE(Ax=2, XVec=X, YVec=sinus, Width=3, Draw=900) END FUNCTION F4() ! plot SIN and COS SET(xmin=0, xmax=6) xtitle = "black: $x1=0.78 and $x2=3.93" DLG(TI="SIN(x) == COS(x)", Rows=2,Cols=2, Ax=4, Min=xmin,Max=xmax,TI=xtitle, Y=0,TI="red=SIN, blue=COS", MIN=-1,Max=1) DO i = 1, 100 X(i) = xmin + i * (xmax - xmin) / 100 sinus(i) = SIN(X(i)) cosinus(i) = COS(X(i)) ENDDO LINE(Ax=4, X=$x1,Y=-1,Draw=-1, Y=1,Width=4,Draw=0) LINE(Ax=4, X=$x2,Y=-1,Draw=-1, Y=1,Width=4,Draw=0) LINE(Ax=4, XV=X, YVec=sinus, W=3, Draw=900, YVec=cosinus, W=3, Draw=9) END #Describe . This sample solves the equations for x . SIN(x) = 0, x <= 0.001 <= 6 . SIN(x) = COS(x), x1 <= 0 <= 3 . SIN(x) = COS(x), x1 <= 3 <= 6 . Call ROOT: . xZero = ROOT("y=func(x)", "dy=dFunc(x)", . [lower, upper, precision, maxIters, ivalParts]) . "y=func(x)" = Function to get roots from (obligatory) . "y=dFunc(x)" = 1st derivative of func (obligatory) . (See script on how to build func and dFunc) . lower = lower bound for root, default=0 (optional) . upper = upper bound for root, default=1 (optional) . precision = Desired accuracy, default=1E-8 (optional) . maxIters = maximum iterations . Results are plotted with functions F3 and F4. . (F functions can be called with its key or by mouse) . The Newton-Raphson algorithm . is used the find the roots of . func(x) = 0 ###